import React from 'react';
import { Table,Button } from 'antd';

// 定义库存数据的类型
interface Inventory {
  inventory_id: number;
  part_id: number;
  quantity: number;
  inbound_time: string;
  outbound_time: string | null;
  batch_number: string;
}

// 生成随机日期的函数
const getRandomDate = (start: Date, end: Date) => {
  return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
};

// 生成虚拟数据
const generateMockData = (count: number): Inventory[] => {
  const data: Inventory[] = [];
  const startDate = new Date(2024, 0, 1); // 随机日期范围开始时间
  const endDate = new Date(); // 随机日期范围结束时间

  for (let i = 1; i <= count; i++) {
    const inboundDate = getRandomDate(startDate, endDate);
    const inboundTime = inboundDate.toISOString().split('T')[0];
    let outboundTime: string | null = null;

    if (Math.random() > 0.5) {
      const outboundDate = getRandomDate(inboundDate, endDate);
      outboundTime = outboundDate.toISOString().split('T')[0];
    }

    data.push({
      inventory_id: i,
      part_id: Math.floor(Math.random() * 100) + 1,
      quantity: Math.floor(Math.random() * 1000),
      inbound_time: inboundTime,
      outbound_time: outboundTime,
      batch_number: `Batch-${Math.floor(Math.random() * 1000)}`
    });
  }
  return data;
};

// 定义表格列
const columns: Table.ColumnsType<Inventory> = [
  {
    title: '库存 ID',
    dataIndex: 'inventory_id',
    key: 'inventory_id'
  },
  {
    title: '零配件 ID',
    dataIndex: 'part_id',
    key: 'part_id'
  },
  {
    title: '库存数量',
    dataIndex: 'quantity',
    key: 'quantity'
  },
  {
    title: '入库时间',
    dataIndex: 'inbound_time',
    key: 'inbound_time'
  },
  {
    title: '出库时间',
    dataIndex: 'outbound_time',
    key: 'outbound_time',
    render: (text) => text || '未出库'
  },
  {
    title: '批次号',
    dataIndex: 'batch_number',
    key: 'batch_number'
  }
];

// 生成 20 条虚拟数据
const mockData = generateMockData(20);

const InventoryList: React.FC = () => {
  return (
    <div style={{ padding: 24 }}>
      <Button type="primary" style={{ marginBottom: 16 }}>新增库存</Button>
      <Table
    size='small'
      columns={columns}
      dataSource={mockData}
      rowKey="inventory_id"
      pagination={{ pageSize: 10 }}
    />
    </div>
  );
};

export default InventoryList;